package org.jeecg.modules.submodules.mapper;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.jeecg.modules.submodules.entity.Car;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.submodules.entity.vo.UserCarVo;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description: 车辆信息表
 * @Author: jeecg-boot
 * @Date:   2020-07-04
 * @Version: V1.0
 */
@Repository
public interface CarMapper extends BaseMapper<Car> {

    @Select("SELECT\n" +
            "car.id carId,\n" +
            "\tcar.CAR_NUMBER,\n" +
            "\tcar.CAR_FRAME_NUMBER,\n" +
            "\tcar.CAR_HITCH_NUMBER,\n" +
            "\tcar.ANNUAL_CHECK_TIME,\n" +
            "\tcar.BUSINESS_INSURANCE_TIME,\n" +
            "\tcar.TRAFFIC_INSURANCE_TIME,\n" +
            "\tcar.CAR_STATUS,\n" +
            "\tsu.id defaultDriverId,\n" +
            "\tsu.realname defaultDriver,\n" +
            "\tGROUP_CONCAT( sub.id ) beforeDriverId,\n" +
            "\tGROUP_CONCAT( sub.realname ) beforeDriver \n" +
            "FROM\n" +
            "\tCar car\n" +
            "\tLEFT JOIN user_car uc ON uc.CAR_ID = car.id\n" +
            "\tLEFT JOIN ( SELECT\n" +
            "\tsu.id,\n" +
            "\tsu.realname \n" +
            "FROM\n" +
            "\tsys_user su\n" +
            "\tLEFT JOIN user_car uc ON uc.USER_ID = su.id\n" +
            "\tLEFT JOIN car ON car.id = uc.CAR_ID \n" +
            "WHERE\n" +
            "\tuc.IS_DEFAULT = 1 \n" +
            "\tAND car.id = #{carId}) su ON su.id = uc.USER_ID\n" +
            "\tLEFT JOIN ( SELECT\n" +
            "\tsu.id,\n" +
            "\tsu.realname \n" +
            "FROM\n" +
            "\tsys_user su\n" +
            "\tLEFT JOIN user_car uc ON uc.USER_ID = su.id\n" +
            "\tLEFT JOIN car ON car.id = uc.CAR_ID \n" +
            "WHERE\n" +
            "\tuc.IS_DEFAULT = 0 \n" +
            "\tAND car.id = #{carId} ) sub ON sub.id = uc.USER_ID \n" +
            "WHERE\n" +
            "\tcar.ID =#{carId}")
    UserCarVo getUserCarVoDetail(String carId);

    @SelectProvider(method = "getListOfUserCarVo" ,type = CarMapperProvider.class)
    List<UserCarVo> getListOfUserCarVo(UserCarVo carVo);


    @SelectProvider(method = "getCarAndDefaultDriver" ,type = CarMapperProvider.class)
    List<UserCarVo> getCarAndDefaultDriver();
}
